<?php
/**
 * sys logger
 * @package kernel_lib_sys
 */
/**
 * sys logger
 * @author pandajingjing
 * @package kernel_lib_sys
 */
class sys_logger{

	/**
	 * 系统日志实例
	 * @var object
	 */
	private static $_objInstance=NULL;

	/**
	 * 配置信息
	 * @var array
	 */
	private $_arrConfig;

	/**
	 * 系统变量
	 * @var object
	 */
	private $_objVari;

	/**
	 * 构造函数
	 */
	private function __construct(){
		$this->_objVari=sys_variable::getInstance();
		$this->_arrConfig=get_config('','logger');
		$strBaseDir=$this->_arrConfig['base_dir'];
		unset($this->_arrConfig['base_dir']);
		foreach($this->_arrConfig as $strKey=>$mixConfig){
			switch($mixConfig['split_type']){
				case 'day':
					$this->_arrConfig[$strKey]['filename']=$strBaseDir.DIRECTORY_SEPARATOR.get_config('fulldomain').DIRECTORY_SEPARATOR.$this->_arrConfig[$strKey]['dir'].DIRECTORY_SEPARATOR.date('Y-m-d',$this->_objVari->getTime()).'.log';
					break;
				case 'week':
					$this->_arrConfig[$strKey]['filename']=$strBaseDir.DIRECTORY_SEPARATOR.get_config('fulldomain').DIRECTORY_SEPARATOR.$this->_arrConfig[$strKey]['dir'].DIRECTORY_SEPARATOR.date('Y-W',$this->_objVari->getTime()).'.log';
					break;
				case 'month':
					$this->_arrConfig[$strKey]['filename']=$strBaseDir.DIRECTORY_SEPARATOR.get_config('fulldomain').DIRECTORY_SEPARATOR.$this->_arrConfig[$strKey]['dir'].DIRECTORY_SEPARATOR.date('Y-m',$this->_objVari->getTime()).'.log';
					break;
				case 'persistent':
					$this->_arrConfig[$strKey]['filename']=$strBaseDir.DIRECTORY_SEPARATOR.get_config('fulldomain').DIRECTORY_SEPARATOR.$this->_arrConfig[$strKey]['file'];
					break;
			}
			$strPath=dirname($this->_arrConfig[$strKey]['filename']);
			if(!is_dir($strPath)){
				if(false===mkdir($strPath,0755,true)){
					trigger_error(__CLASS__.': can not create path('.$strPath.').');
					return false;
				}
			}
		}
	}

	/**
	 * 构造函数
	 */
	private function __clone(){

	}

	/**
	 * 获取实例
	 * @return object
	 */
	static function getInstance(){
		if(!(self::$_objInstance instanceof self)){
			self::$_objInstance=new self();
		}
		return self::$_objInstance;
	}

	/**
	 * 添加日志
	 * @param array $p_arrContent
	 * @param string $p_strClass
	 */
	function addLog($p_arrContent,$p_strClass='common'){
		$strLogFile=$this->_arrConfig[$p_strClass]['filename'];
		$strLog='';
		foreach($p_arrContent as $strTitle=>$strContent){
			$strLog.="\t".$strTitle.': '.$strContent;
		}
		$strLog=date('Ymd H:i:s',$this->_objVari->getTime()).$strLog.PHP_EOL;
		file_put_contents($strLogFile,$strLog,FILE_APPEND);
	}
}